package com.ziyueyuwen.xiniao.base;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

/**
 * @author jun 2018/12/4
 */
public class SpringFactory implements BeanFactoryAware {
    private static BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        SpringFactory.beanFactory = beanFactory;
    }

    public static <T> T getBean(String beanName) {
        if (null != beanFactory) {
            return (T) beanFactory.getBean(beanName);
        }
        return null;
    }

    public static <T> T getBean(Class<T> clazz) {
        if (null != beanFactory) {
            return beanFactory.getBean(clazz);
        }
        return null;
    }
}